Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding height and width attributes #8

Closed
docwhat opened this issue Mar 22, 2018 · 9 comments
Closed

Allow overriding height and width attributes #8

docwhat opened this issue Mar 22, 2018 · 9 comments

Comments

@docwhat
Copy link

docwhat commented Mar 22, 2018

I'm using the config options of { icon: true, viewBox: false }.

When doing (roughly) this:

import CommentIcon from '../icons/comment-alt.svg'

export const Example = props => (
  <div>
    <CommentIcon width="1.125em" /> 
  </div>
)

The final render is (roughly):

<div><svg viewBox="0 0 576 512" width="1em" height="1em">...</path></svg></div>

I expected the width to be 1.125em.

@docwhat
Copy link
Author

docwhat commented Mar 22, 2018

Note: Setting CSS does work around it, but I'd rather have the mark up just be correct out-of-the-box.

@zabute
Copy link
Owner

zabute commented Mar 22, 2018

Looks like this is an upstream issue (tested it in a non-gatsby project just to be sure). It also doesn't seem to respect the viewBox: false option as specified in the svgr docs.

@neoziro any thoughts on this?

@gregberge
Copy link

I think this is because options are passed using query string. Is it possible to provide options using an object instead?

@zabute
Copy link
Owner

zabute commented Mar 23, 2018

Thanks @neoziro. Unfortunately, webpack-configurator only supports query.

Adding options in the loader config throws:

There were errors with your webpack config:
[1]
module.loaders.7.options
object.allowUnknown , "options" is not allowed

Gatsby v2 no longer depends on webpack-configurator, but until it becomes available (which could be a while) we can't use options. Is there any way SVGR could handle options passed in via query?

@gregberge
Copy link

Is it possible to pass false and true using query?

@zabute
Copy link
Owner

zabute commented Mar 23, 2018

I think yes because this is what I get with the options: { ref: true, icon: true, viewBox: false }

 return React.createElement("svg", _extends({
    fill: "none",
    stroke: "currentColor",
    strokeWidth: 2,
    strokeLinecap: "round",
    strokeLinejoin: "round",
    className: "feather feather-search"
  }, props, {
    width: "1em",
    height: "1em",
    ref: svgRef
  }), _ref2, _ref3);
};

Everything is okay. I was also mistaken when I said it wasn't respecting viewBox: false because its been removed.

Also, when I do

<SearchIcon viewBox="0 0 12 12" width="4em" height="4em" />

I get

<svg viewBox="0 0 12 12" width="1em" height="1em">

Other options like { dimensions: false } are also handled properly. So its definitely a problem with width and height

@zabute
Copy link
Owner

zabute commented Mar 23, 2018

I'm taking a look at SVR's source to see if I can track it down.

@zabute
Copy link
Owner

zabute commented Mar 24, 2018

After further investigation, I believe this is a bug with how svgr does the transforms. I edited the webpack.test.js to use query strings instead of objects and it seems to be getting the options correctly.

Even with the cli, --no-icon flag will result in the following component:

 <svg viewBox="0 0 88 88" {...props} width="1em" height="1em">

The width and height attrs are inserted after the rest props are spread which is why we can't override it.

@zabute
Copy link
Owner

zabute commented Mar 25, 2018

Resolved in svgr 1.9.1.

Thanks @neoziro

@zabute zabute closed this as completed Mar 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants